<?php
/**
 * OrderProductRepo.php
 *
 * @copyright  2022 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     Edward Yang <yangjin@guangda.work>
 * @created    2022-07-04 21:14:12
 * @modified   2022-07-04 21:14:12
 */

namespace Beike\Repositories;

use Beike\Models\Order;
use Beike\Models\OrderProduct;
use Beike\Services\StateMachineService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class OrderProductRepo
{
    /**
     * 获取所有客户订单商品列表
     *
     * @param array $filters
     * @return Builder[]|Collection
     */
    public static function filterAll(array $filters = [])
    {
        $builder = static::getListBuilder($filters)->orderByDesc('o.created_at');

        return $builder->get();
    }
    /**
     * @param array $filters
     * @return Builder
     */
    public static function getListBuilder(array $filters = []): Builder
    {
        $builder = OrderProduct::query()->from('order_products as op')
            ->select('o.*','op.product_id','op.name','op.image','op.quantity','op.price','op.product_sku')
            ->join('orders as o','op.order_id','=','o.id','left')
            ->where('o.status', '<>', StateMachineService::CREATED);

        $number = $filters['number'] ?? 0;
        if ($number) {
            $builder->where('o.number', 'like', "%{$number}%");
        }

        $customerName = $filters['customer_name'] ?? '';
        if ($customerName) {
            $builder->where('o.customer_name', 'like', "%{$customerName}%");
        }

        $email = $filters['email'] ?? '';
        if ($email) {
            $builder->where('o.email', 'like', "%{$email}%");
        }

        $phone = $filters['phone'] ?? '';
        if ($phone) {
            $builder->where('o.phone', 'like', "%{$phone}%");
        }

        $customer = $filters['customer'] ?? null;
        if ($customer) {
            $builder->where('o.customer_id', $customer->id);
        }

        $start = $filters['start'] ?? null;
        if ($start) {
            $builder->where('o.created_at', '>', $start);
        }

        $end = $filters['end'] ?? null;
        if ($end) {
            $builder->where('o.created_at', '<', $end);
        }

        $status = $filters['status'] ?? '';
        if ($status) {
            $builder->where('o.status', $status);
        }

        // 回收站
        if (isset($filters['trashed']) && $filters['trashed']) {
            $builder->onlyTrashed();
        }

        return $builder;
    }
    /**
     * 创建商品明细
     *
     * @param Order $order
     * @param $cartProducts
     */
    public static function createOrderProducts(Order $order, $cartProducts)
    {
        $orderProducts = [];
        foreach ($cartProducts as $cartProduct) {
            $productName   = $cartProduct['name'];
            $variantLabels = $cartProduct['variant_labels'] ?? '';
            if ($variantLabels) {
                $productName .= " - {$variantLabels}";
            }
            $orderProducts[] = [
                'product_id'   => $cartProduct['product_id'],
                'order_number' => $order->number,
                'product_sku'  => $cartProduct['product_sku'],
                'name'         => $productName,
                'image'        => $cartProduct['image'],
                'quantity'     => $cartProduct['quantity'],
                'price'        => $cartProduct['price'],
            ];
        }
        $order->orderProducts()->createMany($orderProducts);
    }

    /**
     * 查找单条商品明细数据
     *
     * @param $id
     * @return Builder|Builder[]|Collection|Model|null
     */
    public static function find($id): Model|Collection|Builder|array|null
    {
        return OrderProduct::query()->findOrFail($id);
    }
}
